package com.tushare.controller;

import com.tushare.entity.Daily;
import com.tushare.entity.DailyBasic;
import com.tushare.entity.MoneyFlowHsgt;
import com.tushare.entity.TradeCal;
import com.tushare.network.WapResponse;
import com.tushare.util.ServiceUtil;
import com.tushare.util.TransformClassUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@CrossOrigin("*")
public class StockTradeController {
    private static final String TAG = "ScheduledController";
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private final TransformClassUtil transformClassUtil = new TransformClassUtil();
    @Autowired
    RestTemplate restTemplate;

    /**
     * 日线行情
     */
    @GetMapping("daily")
    public List<Daily> getDaily(@RequestParam(value = "ts_code", required = false) String ts_code,
                                @RequestParam(value = "trade_date", required = false) Integer trade_date,
                                @RequestParam(value = "start_date", required = false) String start_date) {
        ServiceUtil serviceUtil = new ServiceUtil();
        serviceUtil.initWapRequest("daily");
        if (ts_code != null) {
            serviceUtil.appendField("ts_code", ts_code);
        } else {
            if (trade_date != null) {
                serviceUtil.appendField("trade_date", trade_date);
            } else {
                return null;
            }
        }
        if (start_date != null) {
            serviceUtil.appendField("start_date", start_date);
        }
        WapResponse.WapData data = serviceUtil.getNetworkData(restTemplate);
        return data == null ? null : transformClassUtil.initList(data, Daily.class);
    }

    /**
     * 每日指标
     */
    @GetMapping(value = "daily_basic")
    public List<DailyBasic> getDailyBasic(@RequestParam(value = "ts_code", required = false) String ts_code,
                                          @RequestParam(value = "trade_date", required = false) String trade_date,
                                          @RequestParam(value = "start_date", required = false) String start_date) {
        ServiceUtil serviceUtil = new ServiceUtil();
        serviceUtil.initWapRequest("daily_basic");
        if (ts_code != null) {
            serviceUtil.appendField("ts_code", ts_code);
        } else {
            if (trade_date != null) {
                serviceUtil.appendField("trade_date", trade_date);
            } else {
                return null;
            }
        }
        if (start_date != null) {
            serviceUtil.appendField("start_date", start_date);
        }
        WapResponse.WapData data = serviceUtil.getNetworkData(restTemplate);
        return data == null ? null : transformClassUtil.initList(data, DailyBasic.class);
    }

    /**
     * 获取交易日期
     * @return
     */
    @GetMapping(value = "trade_cal")
    public List<TradeCal> getTradeCal(@RequestParam("start_date") int start_date,
                                      @RequestParam("end_date") int end_date) {
        ServiceUtil serviceUtil = new ServiceUtil();
        serviceUtil.initWapRequest("trade_cal");
        serviceUtil.appendField("exchange", "SSE");
        serviceUtil.appendField("start_date", start_date);
        serviceUtil.appendField("end_date", end_date);
        serviceUtil.appendField("is_open", 1);
        WapResponse.WapData data = serviceUtil.getNetworkData(restTemplate);
        return data == null ? null : transformClassUtil.initList(data, TradeCal.class);
    }

    /**
     * 沪深港通资金流向
     *
     * @param start_date 开始日期
     */
    @GetMapping(value = "moneyflow_hsgt", params = {"start_date"})
    public List<MoneyFlowHsgt> getMoneyFlowHsgt(@RequestParam("start_date") String start_date) {
        ServiceUtil serviceUtil = new ServiceUtil();
        serviceUtil.initWapRequest("moneyflow_hsgt");
        serviceUtil.appendField("start_date", start_date);
        WapResponse.WapData data = serviceUtil.getNetworkData(restTemplate);
        return data == null ? null : transformClassUtil.initList(data, MoneyFlowHsgt.class);
    }

}
